package com.ssh.manager.impl;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.ActivityImage;
import com.ssh.entity.ActivitySuperMarket;
import com.ssh.entity.Product;
import com.ssh.manager.ActivityMarketManager;
import com.sun.jmx.snmp.Timestamp;

@Service
public class ActivityMarketManagerImpl implements ActivityMarketManager {
	@Resource
	private IDao daoImpl;

	@Override
	public boolean delete(Long id) {
		try {
			ActivitySuperMarket a = daoImpl.getDomain(ActivitySuperMarket.class, id);
			String sql = "from ActivityImage where activitySuperMarket.id=?";
			List<ActivityImage> aImgs = (List<ActivityImage>)daoImpl.findListBySql(sql, new Object[]{a.getId()}, false, true, false, -1, -1);
			for (int i = 0; i < aImgs.size(); i++) {
				daoImpl.deleteDomain(ActivityImage.class, aImgs.get(i).getId());
			}
			daoImpl.deleteDomain(ActivitySuperMarket.class, id);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public ActivitySuperMarket getById(Long id) {
		return daoImpl.getDomain(ActivitySuperMarket.class, id);
	}

	@Override
	public ActivitySuperMarket saveUpdate(
			ActivitySuperMarket activitySuperMarket) {
		return daoImpl.saveOrUpdateDomain(activitySuperMarket);
	}

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from ActivitySuperMarket";
		String findSql = "from ActivitySuperMarket";
		if (conditionMap.get("activity")!=null && !conditionMap.get("activity").equals("")) {
			sql += " and activity.content like '%"+conditionMap.get("activity")+"%'";
			findSql += " and activity.content like '%"+conditionMap.get("activity")+"%'";
		}
		if (conditionMap.get("market")!=null && !conditionMap.get("market").equals("")) {
			sql += " and superMarket.id="+conditionMap.get("market");
			findSql += " and superMarket.id="+conditionMap.get("market");
		}
		sql = sql.replaceFirst("and", "where");
		findSql = findSql.replaceFirst("and", "where");
		findSql += " order by createDate "+isDesc;
		
		int sum = daoImpl.countAll(sql, null);
		page.setTotalCount(new Long(sum));
		page.countTotalPageNum();
		
		List<ActivitySuperMarket> as = (List<ActivitySuperMarket>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
		page.setPageData(as);
	}
	
	public List<ActivitySuperMarket> findActivityAndMarket(){
        Date date=new Date(System.currentTimeMillis());
		List<ActivitySuperMarket> list=(List<ActivitySuperMarket>)daoImpl.findListBySql("from ActivitySuperMarket where activity.endDate>=?", new Object[]{date}, false, true, false,0,-1);
		
		return list;
	}
	
}
